package com.zengfa.task.server.domain.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zengfa.task.api.enums.JobStatus;
import com.zengfa.task.server.domain.entity.Job;
import com.zengfa.task.server.domain.service.IJobService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class JobServiceImpl implements IJobService {

	@Override
	public Job findJob(String jobGroup, String bizId) {
		/*
		QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
		if(StringUtils.isNotBlank(jobGroup)) {
			queryWrapper.eq("job_group", jobGroup);
		}
		queryWrapper.eq("biz_id", bizId);
		queryWrapper.orderByDesc("created_date");
		List<Job> list = jobMapper.selectList(queryWrapper);
		if(list == null || list.size() < 1) {
			return null;
		}
		return list.get(0);
		*/
		
		return null;
	}

	@Override
	public List<Job> listRunningJobs() {
		/*
		QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", TaskConstant.JOB_STATUS_RUNNING);
		List<Job> list = jobMapper.selectList(queryWrapper);
		return list;
		*/
		
		return null;
	}

	@Override
	public void saveJobStatus(String jobId, JobStatus status, String message) {
		/**
		Job job = jobMapper.selectById(jobId);
		if(job == null) {
			return false;
		}
		job.setStatus(status);
		job.setMessage(message);
		job.setEndTime(new Date());
		job.setUpdatedDate(LocalDateTime.now());

		QueryWrapper<Job> wrapper = new QueryWrapper<>();
		if(TaskConstant.JOB_STATUS_RUNNING.equals(status)) {
			wrapper.eq("status", TaskConstant.JOB_STATUS_NEW);
		}else if(JobUtil.isFinish(status)) {
			wrapper.in("status", TaskConstant.JOB_STATUS_RUNNING,TaskConstant.JOB_STATUS_NEW);
		}
		wrapper.eq("job_id", jobId);
		
		int i = jobMapper.update(job, wrapper);
        log.info("更新任务状态:{},{},{}",jobId,status,i);
        if(i > 0) {
        	return true;
        }
        */
	}

}
